//
//  HMVisitorView.m
//  swift-新浪微博OC版-8-29
//
//  Created by 赵玉忠 on 16/8/31.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "HMVisitorView.h"

@interface HMVisitorView()
@property (nonatomic,strong) UIImageView *iconView;
@property (nonatomic,strong) UIImageView *circleView;
@property (nonatomic,strong) UILabel *messageLabel;
@property (nonatomic,strong) UIButton *registerButton;
@property (nonatomic,strong) UIButton *loginButton;
@property (nonatomic,strong) UIImageView *maskIconView;
@end

@implementation HMVisitorView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}
-(void)setupUI{
//    self.backgroundColor = [UIColor colorWithWhite:237/255 alpha:1];
    self.backgroundColor = [UIColor colorWithWhite:0.929 alpha:1];
//    self.backgroundColor = [UIColor whiteColor];
    // 1. 添加控件
    [self addSubview:self.circleView];
    [self addSubview:self.maskIconView];
    [self addSubview:self.iconView];
    [self addSubview:self.messageLabel];
    [self addSubview:self.registerButton];
    [self addSubview:self.loginButton];
    
    // 2. 添加约束
    //头像
    [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
    }];
    //转圈
    [_circleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(_iconView);
    }];
    //消息label
    [_messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_iconView);
        make.top.equalTo(_circleView.mas_bottom).offset(16);
        make.width.offset(224);
    }];
    // 注册按钮
    [_registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_messageLabel.mas_bottom).offset(16);
        make.left.equalTo(_messageLabel);
        make.width.offset(100);
        make.height.offset(35);
    }];
    // 登录按钮
    [_loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(_messageLabel);
        make.top.equalTo(_registerButton);
        make.width.offset(100);
        make.height.offset(35);
    }];
    //
    [_maskIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self);
        make.bottom.equalTo(_registerButton);
    }];
    
    
    
}
#pragma mark - 懒加载控件
-(UIImageView *)iconView{
    if (_iconView == nil) {
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UIImage imageNamed:@"visitordiscover_feed_image_house"];
        _iconView = imageView;
    }
    return _iconView;
}
-(UIImageView *)circleView{
    if (_circleView == nil) {
    UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UIImage imageNamed:@"visitordiscover_feed_image_smallicon"];
        _circleView = imageView;
    }
    return _circleView;
}
-(UILabel *)messageLabel{
    if (_messageLabel == nil) {
        UILabel *label = [UILabel label:[UIColor darkGrayColor] andwith:14];
        label.text = @"哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈";
        label.numberOfLines = 0;
        label.textAlignment = NSTextAlignmentCenter;
        _messageLabel = label;
    }
    return _messageLabel;
}
-(UIButton *)registerButton{
    if (_registerButton == nil) {
        UIButton *btn = [UIButton button:[UIColor orangeColor] andWith:14];
        [btn setTitle:@"注册" forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"common_button_white"] forState:UIControlStateNormal];
        _registerButton = btn;
    }
    return _registerButton;
}
-(UIButton *)loginButton{
    if (_loginButton == nil) {
        UIButton *btn = [UIButton button:[UIColor darkGrayColor] andWith:14];
        [btn setTitle:@"登录" forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"common_button_white"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
        _loginButton = btn;
    }
    return _loginButton;
}
-(UIImageView *)maskIconView{
    if (_maskIconView == nil) {
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"];
        _maskIconView = imageView;
    }
    return _maskIconView;
}
-(void)loginClick{
    if ([self.delegate respondsToSelector:@selector(visitorViewWillLogin)]) {
        [self.delegate visitorViewWillLogin];
    }
}
#pragma mark -  提供一个方法，供外界设置访客视图里面的信息
-(void)setVisitorViewInfo:(NSString *)imgName andWith:(NSString *)message{
    if (imgName != nil) {
        [self.circleView setHidden:YES];
        self.iconView.image = [UIImage imageNamed:imgName];
    }else{
        [self startAnimation];
    }
    self.messageLabel.text = message;
}
#pragma mark -开启动画
-(void)startAnimation{
    CABasicAnimation *anim = [[CABasicAnimation alloc]init];
    anim.keyPath = @"transform.rotation";
    anim.toValue =@( M_PI * 2);
    anim.duration = 20;
    anim.repeatCount = MAXFLOAT;
    [self.circleView.layer addAnimation:anim forKey:nil];
    
}
@end
